<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/logo1.png" alt=""/>
        <span>智能闸口控制台</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主题区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu background-color="#333744" text-color="#fff"
                 active-text-color="#409eff" :unique-opened="true"
                 :collapse="isCollapse" :collapse-transition="false"
                 :router="true" :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id"
                          @click="saveNavState('/' + subItem.path)">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!--路由占位符-->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>

    import {SESSION_KEY_ACTIVE_PATH} from '../utils/CommonConsts'

    export default {
        data() {
            return {
                // 左侧菜单数据
                menuList: [],
                iconsObj: {
                    125: 'iconfont icon-user',
                    103: 'iconfont icon-tijikongjian',
                    101: 'iconfont icon-shangpin',
                    102: 'iconfont icon-danju',
                    145: 'iconfont icon-baobiao'
                },
                // 是否折叠
                isCollapse: false,
                // 被激活的链接地址
                activePath: ''
            }
        },
        created() {
            this.getMeunList()
            this.activePath = window.sessionStorage.getItem(SESSION_KEY_ACTIVE_PATH)
        },
        methods: {
            /**
             * 登出
             */
            logout() {
                // 清空 sessionStorage
                window.sessionStorage.clear()
                // 重定向到 login界面
                this.$router.push('/login')
            },
            /**
             * 获取左侧所有菜单
             */
            async getMeunList() {
                // 通过解构赋值把Promise的data属性解构出来，并重命名为result
                const {data: result} = await this.$http.get('menus')
                // console.log('getMeunList--:', result)
                if (result.meta.status !== 200) {
                    return this.$message.error(result.meta.msg)
                }
                this.menuList = result.data
            },
            /**
             * 点击按钮，切换菜单栏的折叠与展开
             */
            toggleCollapse() {
                this.isCollapse = !this.isCollapse
            },
            /**
             * 保存当前链接的激活状态（也就是当前连接的index属性的值）
             * @param activePath
             */
            saveNavState(activePath) {
                window.sessionStorage.setItem(SESSION_KEY_ACTIVE_PATH, activePath)
                this.activePath = activePath
            }
        }
    }
</script>

<style lang="less" scoped>
  .home-container {
    height: 100%;
  }

  .el-header {
    background-color: #373d41;
    // 左右贴边对齐
    display: flex;
    justify-content: space-between;
    // 让 logo1.png 贴近左侧
    padding-left: 0;
    // 让右侧按钮居中,不上下贴边
    align-items: center;
    // 将文本设置为白色
    color: #fff;
    // 字体大小
    font-size: 20px;
    // 文本纵向居中 嵌套
    > div {
      display: flex;
      align-items: center;
      // 图片和文本之间设置间距
      span {
        margin-left: 10px;
      }
    }
  }

  .el-aside {
    background-color: #333744;

    .el-menu {
      border-right: none;
    }
  }

  .el-main {
    background-color: #eaedf1;
  }

  .iconfont {
    margin-right: 10px;
  }

  .toggle-button {
    /* 背景颜色 */
    background-color: #4a5064;
    /* 字体 */
    font-size: 10px;
    /* 行高 */
    line-height: 24px;
    color: #fff;
    text-align: center;
    /* 字符间距 */
    letter-spacing: 0.2em;
    /* 鼠标放上去，光标变小手 */
    cursor: pointer;
  }

</style>
